const mysql = require('mysql')
const R = require('ramda')

const hosts = { product: 'localhost', dev: '192.154.214.165' }

const me = {
  get_pool: R.memoize(() => {
    return mysql.createPool({
      connectionLimit : 5,
      host: hosts.product,
      user: 'kobako',
      password: 'qwer1234',
      database: 'kobako'
    })
  }),
  query: (sql, values) => {
    return new Promise((resolve, reject) => {
      const callback = (err, res) => {
        if(err){
          console.error('请求SQL: ' + sql + ' 失败!')
          console.error('err: ' + err)
          resolve(null)
        }else{
          resolve(res)
        }
      }
      const rest_params = R.isNil(values) ? [callback] : [values,callback]
      me.get_pool().query(sql,...rest_params)
    })
  }
}

module.exports = me